package com.ys168.gam.shortestpath.persistent;

import static com.ys168.gam.shortestpath.uimessage.UIMessageUtil.getString;

import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys168.gam.shortestpath.model.Node;
import com.ys168.gam.shortestpath.uimessage.UIMessageCode;
import com.ys168.gam.shortestpath.util.FileUtil;

/**
 *
 * @author Jason Krebs
 * @date 2015年2月7日
 */
public class DataImporter {

    private static final Logger LOGGER = Logger.getLogger(DataImporter.class.getName());
    private static final String NODE_TAG = "node";
    private static final String LINE_TAG = "line";

    /**
     * 导入Node信息
     *
     * @param path
     * @param image
     * @return
     */
    public static List<Node> importFrom(String path, Image image) {
        List<Node> nodes = new ArrayList<Node>();

        String data = FileUtil.loadFrom(path);
        if (data == null || data.isEmpty()) {
            return nodes;
        }

        JSONObject object = JSONObject.parseObject(data);

        JSONArray nodeArray = object.getJSONArray(NODE_TAG);
        Map<Integer, Node> nodesMap = parseNode(nodeArray);

        JSONArray lineArray = object.getJSONArray(LINE_TAG);
        nodes.addAll(parseLine(lineArray, nodesMap));

        for (Node node : nodes) {
            node.setImage(image);
        }

        return nodes;
    }

    public static void main(String[] args) throws IOException {
        String path = "D:/administrator/Desktop/tttt.json";
        LOGGER.log(Level.INFO, "" + importFrom(path, ImageIO.read(DataImporter.class.getResource("/image/Runner.png"))));
    }

    private static List<Node> parseLine(JSONArray lineArray, Map<Integer, Node> nodesMap) {
        List<Node> nodes = new ArrayList<Node>();
        if (lineArray != null) {
            for (int i = 0; i < lineArray.size(); i++) {
                JSONObject line = lineArray.getJSONObject(i);

                int start = line.getIntValue("start");
                int end = line.getIntValue("end");

                Node startNode = nodesMap.get(start);
                Node endNode = nodesMap.get(end);

                if (startNode == null || endNode == null) {
                    continue;
                }
                startNode.addOutNode(endNode);
                endNode.addInNode(startNode);
            }
        }
        nodes.addAll(nodesMap.values());

        return nodes;
    }

    private static Map<Integer, Node> parseNode(JSONArray nodeArray) {
        Map<Integer, Node> nodes = new HashMap<Integer, Node>();
        for (int i = 0; i < nodeArray.size(); i++) {
            JSONObject nodeObject = nodeArray.getJSONObject(i);
            Node node = JSONObject.parseObject(nodeObject.toJSONString(), Node.class);
            if (node != null) {
                nodes.put(node.getIndex(), node);
            }
        }
        LOGGER.log(Level.INFO, String.format(getString(UIMessageCode.LOG_03), nodes.size()));
        return nodes;
    }
}
